package wangwenjun.phase2.concurrencypattern.activeobject.staticproxy.framework;

import java.util.LinkedList;

/**
 * @author ChangLiang
 * @date 2020/6/17
 */
class OrderServiceMessageQueue {

    /**
     * 用于存放提交的OrderServiceMessage 消息
     */
    private final LinkedList<OrderServiceMessage> messageList = new LinkedList<>();

    public OrderServiceMessageQueue() {
        new OrderServiceDaemonThread(this).start();
    }

    public void offer(OrderServiceMessage orderServiceMessage) {
        synchronized (this) {
            messageList.addLast(orderServiceMessage);
            // 因为只有一个线程负责take 数据， 因此没有必要使用notifyAll 方法
            this.notify();
        }
    }

    public OrderServiceMessage take() {
        synchronized (this) {
            while (messageList.isEmpty()) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            OrderServiceMessage message = messageList.removeFirst();
            return message;
        }
    }
}
